<template>
  <div class="appointments-container">
    <el-card class="search-card">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="学员姓名">
          <el-input v-model="searchForm.studentName" placeholder="请输入学员姓名" clearable />
        </el-form-item>
        <el-form-item label="教练姓名">
          <el-input v-model="searchForm.coachName" placeholder="请输入教练姓名" clearable />
        </el-form-item>
        <el-form-item label="预约日期">
          <el-date-picker
            v-model="searchForm.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="预约状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="待确认" value="待确认" />
            <el-option label="已确认" value="已确认" />
            <el-option label="已完成" value="已完成" />
            <el-option label="已取消" value="已取消" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>查询
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <template #header>
        <div class="card-header">
          <span>预约列表</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>新增预约
          </el-button>
        </div>
      </template>

      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="studentName" label="学员姓名" />
        <el-table-column prop="coachName" label="教练姓名" />
        <el-table-column prop="subject" label="培训科目" />
        <el-table-column prop="appointmentDate" label="预约日期" />
        <el-table-column prop="timeSlot" label="时间段" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template #default="{ row }">
            <el-button-group>
              <el-button 
                v-if="row.status === '待确认'"
                type="success" 
                link 
                @click="handleConfirm(row)"
              >
                <el-icon><Check /></el-icon>确认
              </el-button>
              <el-button 
                v-if="['待确认', '已确认'].includes(row.status)"
                type="danger" 
                link 
                @click="handleCancel(row)"
              >
                <el-icon><Close /></el-icon>取消
              </el-button>
              <el-button 
                v-if="row.status === '已确认'"
                type="primary" 
                link 
                @click="handleComplete(row)"
              >
                <el-icon><Check /></el-icon>完成
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      title="新增预约"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="学员" prop="studentId">
          <el-select
            v-model="form.studentId"
            placeholder="请选择学员"
            @change="handleStudentChange"
          >
            <el-option
              v-for="student in studentList"
              :key="student.id"
              :label="student.name"
              :value="student.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="教练" prop="coachId">
          <el-select
            v-model="form.coachId"
            placeholder="请选择教练"
            :disabled="!form.studentId"
          >
            <el-option
              v-for="coach in coachList"
              :key="coach.id"
              :label="coach.name"
              :value="coach.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="预约日期" prop="appointmentDate">
          <el-date-picker
            v-model="form.appointmentDate"
            type="date"
            placeholder="请选择日期"
            :disabled="!form.coachId"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="时间段" prop="timeSlot">
          <el-select
            v-model="form.timeSlot"
            placeholder="请选择时间段"
            :disabled="!form.appointmentDate"
          >
            <el-option label="08:00-10:00" value="08:00-10:00" />
            <el-option label="10:00-12:00" value="10:00-12:00" />
            <el-option label="14:00-16:00" value="14:00-16:00" />
            <el-option label="16:00-18:00" value="16:00-18:00" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Search,
  Refresh,
  Plus,
  Check,
  Close
} from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  studentName: '',
  coachName: '',
  date: [],
  status: ''
})

// 表格数据
const loading = ref(false)
const tableData = ref([
  {
    studentName: '张三',
    coachName: '李教练',
    subject: '科目二',
    appointmentDate: '2024-03-15',
    timeSlot: '08:00-10:00',
    status: '待确认'
  },
  {
    studentName: '李四',
    coachName: '王教练',
    subject: '科目三',
    appointmentDate: '2024-03-15',
    timeSlot: '10:00-12:00',
    status: '已确认'
  },
  {
    studentName: '王五',
    coachName: '张教练',
    subject: '科目二',
    appointmentDate: '2024-03-16',
    timeSlot: '14:00-16:00',
    status: '已完成'
  },
  {
    studentName: '赵六',
    coachName: '李教练',
    subject: '科目二',
    appointmentDate: '2024-03-16',
    timeSlot: '16:00-18:00',
    status: '已取消'
  },
  {
    studentName: '孙七',
    coachName: '王教练',
    subject: '科目三',
    appointmentDate: '2024-03-17',
    timeSlot: '08:00-10:00',
    status: '待确认'
  }
])

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 表单
const dialogVisible = ref(false)
const formRef = ref(null)
const form = reactive({
  studentId: '',
  coachId: '',
  appointmentDate: '',
  timeSlot: '',
  remark: ''
})

// 学员列表
const studentList = ref([
  { id: 1, name: '张三' },
  { id: 2, name: '李四' },
  { id: 3, name: '王五' },
  { id: 4, name: '赵六' },
  { id: 5, name: '孙七' },
  { id: 6, name: '周八' },
  { id: 7, name: '吴九' },
  { id: 8, name: '郑十' }
])

// 教练列表
const coachList = ref([
  { id: 1, name: '李教练' },
  { id: 2, name: '王教练' },
  { id: 3, name: '张教练' },
  { id: 4, name: '赵教练' },
  { id: 5, name: '孙教练' }
])

// 表单验证规则
const rules = {
  studentId: [{ required: true, message: '请选择学员', trigger: 'change' }],
  coachId: [{ required: true, message: '请选择教练', trigger: 'change' }],
  appointmentDate: [{ required: true, message: '请选择预约日期', trigger: 'change' }],
  timeSlot: [{ required: true, message: '请选择时间段', trigger: 'change' }]
}

// 获取状态类型
const getStatusType = (status) => {
  const statusMap = {
    '待确认': 'warning',
    '已确认': 'primary',
    '已完成': 'success',
    '已取消': 'info'
  }
  return statusMap[status] || 'info'
}

// 搜索
const handleSearch = () => {
  // TODO: 实现搜索逻辑
  console.log('search:', searchForm)
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = key === 'date' ? [] : ''
  })
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogVisible.value = true
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
}

// 确认预约
const handleConfirm = (row) => {
  ElMessageBox.confirm(
    '确认该预约吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'info',
    }
  ).then(() => {
    // TODO: 实现确认逻辑
    ElMessage.success('预约已确认')
  }).catch(() => {})
}

// 取消预约
const handleCancel = (row) => {
  ElMessageBox.confirm(
    '确认取消该预约吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 实现取消逻辑
    ElMessage.success('预约已取消')
  }).catch(() => {})
}

// 完成预约
const handleComplete = (row) => {
  ElMessageBox.confirm(
    '确认完成该预约吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'info',
    }
  ).then(() => {
    // TODO: 实现完成逻辑
    ElMessage.success('预约已完成')
  }).catch(() => {})
}

// 学员选择变化
const handleStudentChange = (value) => {
  form.coachId = ''
  form.appointmentDate = ''
  form.timeSlot = ''
  // TODO: 根据学员获取对应教练列表
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      // TODO: 实现提交逻辑
      console.log('submit:', form)
      ElMessage.success('预约添加成功')
      dialogVisible.value = false
    }
  })
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
</script>

<style scoped>
.appointments-container {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>